Hello, everyone. See you again. I'm Quan stack Jun.
Service governance is arguably the most core and fundamental module in the micro-service architecture. It mainly implements the automatic registration and discovery of individual micro-service instances.
Spring Cloud Eureka is part of the Spring Cloud Netflix microservice suite and is responsible for completing the service governance functions in the microservice architecture.
This article uses a simple example to share how to use Eureka for service governance:
- Set up Service Registration Center
- Register Service Provider
- Service Discovery and Consumption
===========I am a gorgeous dividing line======================
1. Set up a service registration center
First list the full directory structure:

The construction process is as follows:
- Create maven project: Eureka (implementation omitted)
- Modify the pom file to introduce dependencies
copy<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>eureka</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <!-- Use dependencyManagement Version management --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Introduce eureka server rely on --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> </project>
- Create Startup Class
copy/** * * @EnableEurekaServer * Service registry used to specify this project as Eureka */ @EnableEurekaServer @SpringBootApplication public class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); } }
- Configure application.properties file
copy#Setting tomcat service port number server.port=1111 #Set Service Name spring.application.name=eureka-service eureka.instance.hostname=localhost #The registry does not need to register itself eureka.client.register-with-eureka=false #Registry does not need to discover services eureka.client.fetch-registry=false #Set the URL of the Service Registry eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
- Start the service and visit it. We'll see this:

2. Registering Service Providers
List the complete directory structure first:

The construction process is as follows:
- Create maven project: hello-service (implementation omitted)
- Modify the pom file to introduce dependencies
copy<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>hello-service</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Introduce eureka Client Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> </project>
- Create Startup Class
copy/*** * * @EnableDiscoveryClient * Let services use eureka servers * Implement service registration and discovery * */ @EnableDiscoveryClient @SpringBootApplication public class HelloApp { public static void main(String[] args) { SpringApplication.run(HelloApp.class, args); } }
- Create controller
copy@RestController public class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); //Service id of print service logger.info("*********" + instance.getServiceId()); return "hello,this is hello-service"; } }
- Configure application.properties file
copyserver.port=9090 #Set Service Name spring.application.name=hello-service #Set the URL of the service registry to which this service will register itself eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
Start and test
- ) After booting, the console of hello-service will read like this (xxx stands for your PC name)
copyRegistered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
The console of eureka prints the following (xxx stands for your PC name)
copyRegistered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
- ) Visit localhost:1111 again, and you will find that the service is registered with the registry

3. Service Discovery and Consumption
The complete directory structure is as follows:

Setup process:
- Create maven project (implementation omitted)
- Modify the pom file to introduce dependencies
copy<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.sam</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Introduce eureka Client Dependency --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Introduce ribbon Dependency, to achieve load balancing, we're just using it here, let's not introduce it yet--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> </project> This is more than hello-service Service providers, much more ribbon Dependency on
Create Startup Class
copy@EnableDiscoveryClient @SpringBootApplication public class ConsumerApp { //@Bean is used on methods to set method return value to bean @Bean @LoadBalanced //@LoadBalanced for load balancing public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); } }
copyThis is also used here@EnableDiscoveryClient, Let the service use eureka Server for service registration and discovery
- Create controller
copy@RestController public class ConsumerController { //The restTemplate injected here is at com. Instances configured with @Bean in sam.ConsumerApp @Autowired RestTemplate restTemplate; @RequestMapping("/hello-consumer") public String helloConsumer() { //Call the hello-service service, note that the service name is used here, not the specific ip+port restTemplate.getForObject("http://hello-service/hello", String.class); return "hello consumer finish !!!"; } }
- Configure application.properties file
copyserver.port=9999 spring.application.name=hello-consumer eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka #The configuration project here is the same as the service provider hello-service
- Start, test
- ) Start eureka. To show a balanced effect, our hello-service starts two services, starting two services as follows
The above is the start-up step of hello-service 1 with port number 9090; Similarly, set hello-service 2 with port number 9091 (implementation omitted).
- ) Start hello-consumer
- ) Visit again http://localhost:1111/ , you will find two hello-service services (port number one is 9090, one is 9091), and one hello-consume service
- ) Multiple visits http://localhost:9999/hello-consumer, you will find that hello-service 1 and hello-service 2 are invoked in turn (responsible for balance has been achieved) and can be confirmed by the console print content for both (remember that we have loggerlogger.info in the hello-service controller ("*******"+ instance.getServiceId()); Are you? Yes, that's the print)
- ) Start eureka. To show a balanced effect, our hello-service starts two services, starting two services as follows
4. Summary
The above example implements basic service governance:
- Implement Service Registry via spring-cloud-starter-eureka-server and @EnableEurekaServer
- Use and register with the service registry via spring-cloud-starter-eureka and @EnableDiscoveryClient
- Use the registry and discover services through spring-cloud-starter-eureka and @EnableDiscoveryClient, and load-balanced consumer services through spring-cloud-starter-ribbon
PS: The IDE I use here is Spring Tool Suite, which is a customized version of eclipse for spring. It is convenient for us to use spring for development. Interested friends can learn about it by themselves in Baidu.
Publisher: Full stack programmer stack length, please specify the source: https://javaforall.cn/120909.html Original Link: https://javaforall.cn