Service governance can be said to be the most core and basic module in the microservice architecture. It is mainly used to realize the automated registration and discovery of various microservice instances.
Spring Cloud Eureka is part of the Spring Cloud Netflix microservices suite, which is mainly responsible for completing service governance functions in the microservice architecture.
This article uses simple small examples to share how to manage service through Eureka:
============== I am the gorgeous dividing line================================
1. Build a service registration center
First list the complete directory structure:
The construction process is as follows:
1. Create a maven project: eureka (specific implementation omitted)
2. Modify the pom file and introduce dependencies
<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 for 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> </dependency> </dependencyManagement> <dependencies> <!-- Introducing eureka server dependencies--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependency> </dependencies></project>
3. Create a startup class
/** * * @EnableEurekaServer * The service registration center used to specify that the project is Eureka*/@EnableEurekaServer@SpringBootApplicationpublic class EurekaApp { public static void main(String[] args) { SpringApplication.run(EurekaApp.class, args); }} 4. Configure the application.properties file
#Set the tomcat service port number server.port=1111#Set the service name spring.application.name=eureka-serviceeureka.instance.hostname=localhost#Register center does not need to register itself eureka.client.register-with-eureka=false#Register center does not need to discover the service eureka.client.fetch-registry=false#Set the URL of the service registration center Leureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka 5. Start the service and access it, and we will see a picture like this:
2. Register a service provider
First list the complete directory structure:
The construction process is as follows:
1. Create a maven project: hello-service (specific implementation omitted)
2. Modify the pom file and introduce dependencies
<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> </dependency> </dependencyManagement> <dependencies> <!-- Introducing eureka client dependencies--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependency> </dependencies></project>
3. Create a startup class
/*** * * @EnableDiscoveryClient * Let the service use eureka server* to implement service registration and discovery* */@EnableDiscoveryClient@SpringBootApplicationpublic class HelloApp { public static void main(String[] args) { SpringApplication.run(HelloApp.class, args); }}4. Create a controller
@RestControllerpublic class HelloController { Logger logger = LoggerFactory.getLogger(HelloController.class); @Autowired DiscoveryClient discoveryClient; @RequestMapping("/hello") public String hello() { ServiceInstance instance = discoveryClient.getLocalServiceInstance(); //Print service id logger.info("*********" + instance.getServiceId()); return "hello,this is hello-service"; }} 5. Configure the application.properties file
server.port=9090#Set the service name spring.application.name=hello-service#Set the URL of the service registration center. This service must register itself with the service registration center eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka
6. Start and test
1.) After starting, the hello-service console will have this word (xxx represents your PC name)
Copy the code as follows: Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
The following words will be printed on the eureka console (xxx represents your PC name)
Copy the code as follows: Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)
2.) Visit localhost:1111 again and you will find that there are services registered in the registration center
3. Service discovery and consumption
The complete directory structure is as follows:
Construction process:
1. Create a maven project (specific implementation omitted)
2. Modify the pom file and introduce dependencies
<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> </dependency> </dependencyManagement> <dependencies> <!-- Introduce eureka client dependencies--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- Introduce ribbon Dependency is used to achieve load balancing. We are just using it here, so we won't introduce it elsewhere --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies></project>
There are more ribbon dependencies than hello-service service providers here
3. Create a startup class
@EnableDiscoveryClient@SpringBootApplicationpublic class ConsumerApp { //@Bean is applied on the method and used to set the method return value to be bean @Bean @LoadBalanced //@LoadBalanced to achieve load balancing public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerApp.class, args); }}@EnableDiscoveryClient should also be used here to let the service use the eureka server to realize service registration and discovery
4. Create a controller
@RestControllerpublic class ConsumerController { //The restTemplate injected here is an instance configured via @Bean in com.sam.ConsumerApp @Autowired RestTemplate restTemplate; @RequestMapping("/hello-consumer") public String helloConsumer() { //Calling 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 !!!"; }}5. Configure the application.properties file
server.port=9999spring.application.name=hello-consumereureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka#The configuration project here is the same as the service provider hello-service
6. Start, test 1.) Start eureka. In order to show the effect of being responsible for balance, our hello-service starts two services. The specific steps for starting two services are as follows
The above is the startup step of hello-service1, with the port number 9090; the same method is set to hello-service2, with the port number 9091 (the specific implementation is omitted).
2.) Start hello-consumer
3.) Visit http://localhost:1111/ again and you will find that there are 2 hello-service services (port numbers are 9090 and 9091) and 1 hello-consume service
4.) After visiting http://localhost:9999/hello-consumer multiple times, you will find that hello-service1 and hello-service2 will be called in turn (responsible for balance has been achieved), and you can confirm the content through the console of both (remember that we have a loggerlogger.info("*********" + instance.getServiceId()); in the controller of hello-service? Yes, this is the printing)
4. Summary
The above examples realize basic service governance:
PS: Let me explain here that the IDE I use is Spring Tool Suite, which is the customized version of spring eclipse, which is convenient for us to use spring for development. Interested friends can learn about it on Baidu.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.