Taught SpringCloud learning with you to build entry-IDEA project (c)

      This blog is to undertake on a "taught you how to build SpringCloud entry item (b) with IDEA", unclear please go to my blog space and then look to see this blog, the above two blog successfully created a simple SpringCloud project, this blog is the main part of the project posted the code to allow readers to better combat operations SpringCloud project to build one of their own

1) substantially follows the framework of the project

2) eureka-server based program startup module

 

package com.xu.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

3) the properties file into yml types of document, and editing the configuration file as follows

application.yml file editing as follows:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost    #表示这个注册中心在本地
  client:
    registerWithEureka: false   #注册中心不注册自己,默认是注册自己
    fetchRegistry: false
    serviceUrl:
      #拿到上面的主机地址和端口地址,配置一个注册中心
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

4) Start the registry service, the console output to see this information, it indicates registry service starts successfully

5) After the registry service starts, enter the address in the browser: http: // localhost: 8761 /, you should see this screen, where you can see some of the service registered in the registry

6) Service Provider service-provider modules other codes follows

ServiceProviderApplication.java
@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {

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

}

修改application.properties文件为application.yml,并编辑如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/    #这里是告诉他注册中心地址
server:
  port: 8763
spring:
  application:
    name: service-hello    #这里给服务取一个帅气的名字

Here only provides a RestApi interface services, followed by a service consumer invokes this test API services:

HelloWorldController.java

package com.xu.serviceprovider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi " + name + ", i am from port: " + port;
    }

}

 

Start this module

Then refresh the registry address HTTP: // localhost: 8761 / , you can see that he has been registered to the registration center

7) Consumer Services Other service-consumer module code is as follows:

ServiceConsumerApplication.java

package com.xu.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceConsumerApplication {

    public static void main(String[] args) {

        SpringApplication.run(ServiceConsumerApplication.class, args);
    }

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {

        return new RestTemplate();
    }
}

service-consumer application.yml module are as follows:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8764
spring:
  application:
    name: service-ribbon

Other codes are as follows:

HelloService.java

package com.xu.serviceconsumer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HelloService {

    @Autowired
    RestTemplate restTemplate;

    public String hiService(String name)
    {
        return restTemplate.getForObject("http://SERVICE-HELLO/hi?name=" + name, String.class);
    }
}

HelloControler.java

package com.xu.serviceconsumer.controller;

import com.xu.serviceconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloControler {

    @Autowired
    HelloService helloService;

    @RequestMapping(value = "/hi")
    public String hi(@RequestParam String name) {
        return helloService.hiService(name);
    }
}

After starting the module, success is as follows:

Refresh Registration Center Address: HTTP: // localhost: 8761 / , you can see a new service has been registered with the registry

Open your browser and enter the address in a new window: HTTP: // localhost: 8764 / ADMIN hi name =? , If successful, it returns the following information indicates a successful call to the service provider's service interface (depending on the port can know, 8763 port is the port service providers)

This blog so far, any questions please leave a message with everyone to discuss learning, encourage each other gentlemen!

Guess you like

Origin www.cnblogs.com/xulijun137/p/12209721.html
Recommended