Nacos+openfeign

1 Introduction

 

Nacos is dedicated to helping you discover, configure and manage microservices. Nacos provides a set of easy-to-use feature sets to help you quickly realize dynamic service discovery, service configuration, service metadata, and traffic management.

2. Preparation

2.1. Install MySQL8

From Nacos 1.3.1 version, the database storage has been upgraded to 8.0, and it is backward compatible

2.2. Install Nacos

http://ip:port number/nacos

 

Start configuration management

  1. Add dependencies:
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>${latest.version}</version>
    </dependency>

 Configure the address of Nacos server in  application.properties  :

server.port=8070
spring.application.name=service-provider
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

Note: The spring.application.name configured here   can also be viewed in the Nacos service list after starting the service

 2. Enable the service registration discovery function through the Spring Cloud native annotation  @EnableDiscoveryClient :

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

	public static void main(String[] args) {
		SpringApplication.run(NacosProviderApplication.class, args);
	}

	@RestController
	class EchoController {
		@RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
		public String echo(@PathVariable String string) {
			return "Hello Nacos Discovery " + string;
		}
	}
}

3. Add a consumer:

Configure the service consumer so that the service consumer can obtain the service it wants to call from the Nacos server through the service registration discovery function of Nacos.

Configure the address of Nacos server in  application.properties  :

server.port=8080
spring.application.name=service-consumer

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

 Enable the service registration discovery function through the Spring Cloud native annotation  @EnableDiscoveryClient . Add the @LoadBalanced  annotation  to  the RestTemplate  instance  to enable the integration of @LoadBalanced  and  Ribbon  :

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }
}

4. Start  ProviderApplication  and  ConsumerApplication  , call  http://localhost:8080/echo/2018 , and the returned content is  Hello Nacos Discovery 2018 .


Join openFeign 

<!-- 添加 openfeign 框架依赖 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
package com.formiss.feign.service;

@FeignClient(value = "service-provider",path = "/provider")
public interface FeignClientService {

    @GetMapping("/echo")
    String echo();
}

@RestController
@RequestMapping(path = "/provider")
public class ProviderController {

    @GetMapping (value = "/echo")
    public String echo() {
        return "Hello Nacos Discovery " ;
    }
}

@SpringBootApplication
@EnableFeignClients(value = "com.formiss.feign.service")
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
}

@RestController
@RequestMapping("/comsumer")
public class UserController {

    @Autowired
    FeignClientService feignClientService;

    @GetMapping("/echo")
    public String echo(){
        return feignClientService.echo();
    }
}

After joining openfeign, the @EnableDiscoveryClient annotation is not needed

Guess you like

Origin blog.csdn.net/JemeryShen/article/details/126349035