SpringBoot_eureka入门记录

建立eureka服务端
1.通过idea新建项目中的springboot脚手架新建一个项目,过程中勾选spirngboot eureka server(具体忘了,简单描述下)

2。覆盖默认配置文件,修改application.yml

server:
  port: 10086 # 端口
spring:
  application:
    name: hhd-server # 应用名称,会在Eureka中显示
eureka:
  client:
    service-url: # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。
      defaultZone: http://127.0.0.1:${server.port}/eureka

3.修改引导类,在类上添加@EnableEurekaServer注解:

@SpringBootApplication
@EnableEurekaServer
public class CloudEurekaApplication {

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

}

4.启动服务,并访问:http://127.0.0.1:10086
SpringBoot_eureka入门记录_第1张图片
至此eureka服务端已搭建完毕。

将服务提供项目加入到微服务群中

项目为一个简单的springboot项目,提供一个http接口
http://localhost:8081/user/1
输出到网页一串json数据

1.导入eureka客户端依赖,下面三组处于不同的标签中

<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>


<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>



	<!--统一管理的依赖版本号-->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2.修改项目配置文件

server:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    username: root
    password: root
  application:			//此处是与eureka有关的配置
    name: service-provider		//此处是与eureka有关的配置

mybatis:
  type-aliases-package: com.example.domain

eureka:		//此处是与eureka有关的配置
  client:
    service-url:
      defaultZone: http://localhost:10086/eureka

3.启动类中加上@EnableDiscoveryClient注解,注册服务

@SpringBootApplication
@MapperScan("com.example.mapper")
@EnableDiscoveryClient
public class CloudProviderApplication {

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

}

4.重新运行启动类,可在上方服务端的访问地址中看到相关信息
http://localhost:10086
SpringBoot_eureka入门记录_第2张图片

服务调用

package com.example.controller;

import com.example.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author hhd
 * @Date 2020/5/5 22:13
 */
@RestController
@RequestMapping("/consumer/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping
    public User findById(@RequestParam("id") Long id){
        List<ServiceInstance> instances = discoveryClient.getInstances("SERVICE-PROVIDER");
        ServiceInstance instance = instances.get(0);
        int port = instance.getPort();
        String host = instance.getHost();
        return this.restTemplate.getForObject("http://"+host+":"+port+"/user/"+id, User.class);
    }
}

你可能感兴趣的:(经验记录)